package com.fjrcloud.controller;

import com.fjrcloud.entity.HouseholdMaster;
import com.fjrcloud.entity.OnTimeReward;
import com.fjrcloud.entity.ResponseBean;
import com.fjrcloud.entity.ResponseFactory;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.service.HouseholdService;
import com.fjrcloud.service.OnTimeRewardService;
import com.fjrcloud.utils.StatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by shengjian.lin on 17-2-11.
 */
@Controller
@RequestMapping("api/reward")
public class OnTimeRewardController {

    @Autowired
    private OnTimeRewardService service;
    @Autowired
    private HouseholdService householdService;

    @RequestMapping("save")
    @ResponseBody
    public ResponseBean save(OnTimeReward entity,Integer masterId) throws BusinessException {
        return ResponseFactory.getResponseBean(StatusUtils.Success,service.save(entity,masterId));
    }
    @RequestMapping("get")
    @ResponseBody
    public ResponseBean get(Integer id, Integer masterId) throws BusinessException {
        if (id == null){

            if (masterId == null)
                throw new BusinessException("无认定主体");
            HouseholdMaster master = householdService.getMaster(masterId);
            if (master == null)
                throw new BusinessException("认定主体无效");

            return ResponseFactory.getResponseBean(StatusUtils.Success,service.get(master));
        }else{
            return ResponseFactory.getResponseBean(StatusUtils.Success,service.get(id));
        }
    }
    @RequestMapping("del")
    @ResponseBody
    public ResponseBean del(Integer id){
        service.del(id);
        return ResponseFactory.getResponseBean(StatusUtils.Success);
    }
}
